package snail.data;

import java.util.List;
import org.hibernate.Query;
import snail.model.User;

public class UserDAO
        extends GenericHibernateDAO<User, Integer>
        implements IUserDAO {

    public User validateUser(String email, String password) {
        Query query = getSession().getNamedQuery(IUserDAO.QUERY_VALIDATE);
        query.setParameter("email", email);
        query.setParameter("password", password);

        Object o = query.uniqueResult();

        if(o == null)
            return null;
        
        return (User) o;
    }

    public User getByEmail(String email) {
        Query query = getSession().getNamedQuery(IUserDAO.QUERY_BYEMAIL);
        query.setParameter("email", email);

        Object o = query.uniqueResult();

        if(o == null)
            return null;

        return (User) o;
    }

    public void deleteUser(int id){
        Query query = getSession().getNamedQuery(IUserDAO.QUERY_DELETE);
        query.setParameter("id", id);
        
        query.executeUpdate();
    }

    @Override
    public List<User> getAll() {
        Query q = getSession().getNamedQuery(IUserDAO.QUERY_ALL);
        return  (List<User>) q.list();
    }

}
